import sys
import pygame
from pygame.locals import *

Ev_QUIT = -1
Ev_IDLE = 0
Ev_ELSE = 1
Ev_MOUSEBTND = MOUSEBUTTONDOWN
Ev_MOUSEMOVE = MOUSEMOTION
Ev_MOUSEBTNU = MOUSEBUTTONUP
Ev_KEYDOWN = KEYDOWN

Area_TVOID = 1
Area_TGAME = 2
Area_TMENU = 4
Area_TALL = 7

class area:

    m_type = None
    m_rect = None
    m_ID = None
    m_x_min = None
    m_x_max = None
    m_y_min = None
    m_y_max = None
    
    def __init__(self,x,y,w,h,id,type):
        self.m_rect = pygame.Rect(x,y,w,h)
        self.m_type = type
        self.m_ID = id
        
        #set boundaries
        self.m_x_min = x
        self.m_x_max = x + w
        self.m_y_min = y
        self.m_y_max = y + h

class area_controller:
    
    m_areas = None
    m_parent = None
    m_idc = None        #ID/area counter
    
    def __init__(self, parent):
        
        self.m_parent = parent;
        self.m_areas = {-1:area(0,0,0,0,-1,Area_TVOID)}
        self.m_idc = 0
        
    def add_area(self, x=0, y=0, w=0, h=0, type=Area_TVOID):
        self.m_areas[self.m_idc] = area(x,y,w,h,self.m_idc, type)
        self.m_idc+=1
   
    def check_select(self,types=Area_TALL):
        for key in self.m_areas :
            if self.m_areas[key].m_type & types :
                if ( self.m_areas[key].m_x_min == self.m_parent.m_Mousex ) and ( self.m_areas[key].m_y_min == self.m_parent.m_Mousey ) :
                    return self.m_areas[key].m_ID
                if ( self.m_areas[key].m_x_max == self.m_parent.m_Mousex ) and ( self.m_areas[key].m_y_min == self.m_parent.m_Mousey ) :
                    return self.m_areas[key].m_ID
                if self.m_areas[key].m_x_min < self.m_parent.m_Mousex :
                    if self.m_areas[key].m_y_min < self.m_parent.m_Mousey :
                        if self.m_areas[key].m_x_max > self.m_parent.m_Mousex :
                            if self.m_areas[key].m_y_max > self.m_parent.m_Mousey :
                                return self.m_areas[key].m_ID
                
        return -1
        
        
    
    def remove_area(self, id):
        del self.m_areas[id]
        
class _input:
    
    m_Mousex = 0
    m_Mousey = 0
    m_Mkey1 = False
    m_Mkey2 = False
    m_Mkey3 = False
    m_Mwheel = 0
    
    m_Key = None
  
    logger = None
    
    Area = None
    
    m_action = False
# --------------------------------------------------------------------------------------
    def __init__(self, log):
# --------------------------------------------------------------------------------------
        self.m_Mousex, self.m_Mousey = pygame.mouse.get_pos()
        self.logger = log
        self.logger.log.info("Initializing Input")
        self.Area = area_controller(self)
        
# --------------------------------------------------------------------------------------       
    def parse(self):
# -------------------------------------------------------------------------------------- 
        self.m_action = False
        for event in pygame.event.get():
            if event.type == QUIT:
                sys.exit(0)
            
            elif event.type == KEYDOWN:
                self.m_action = False
                self.m_Key = event.key
                
                if event.key == K_ESCAPE:
                    sys.exit(0)
               
            elif event.type == MOUSEBUTTONDOWN:
                self.m_action = True
                if event.button == 1:
                    self.m_Mkey1 = True
                elif event.button == 2:
                    self.m_Mkey2 = True
                elif event.button == 3:
                    self.m_Mkey3 = True
                elif event.button == 4:
                    self.m_Mwheel = 1
                elif event.button == 5:
                    self.m_Mwheel = -1
                
            elif event.type == MOUSEBUTTONUP:
                self.m_action = True
                if event.button == 1:
                    self.m_Mkey1 = False
                elif event.button == 2:
                    self.m_Mkey2 = False
                elif event.button == 3:
                    self.m_Mkey3 = False
                
            elif event.type == MOUSEMOTION:
                self.m_Mousex, self.m_Mousey = pygame.mouse.get_pos()
            
            pygame.display.update()
            
        return Ev_IDLE        
        